package com.travelshare.web.controller.business;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.travelshare.common.annotation.Log;
import com.travelshare.common.core.controller.BaseController;
import com.travelshare.common.core.domain.AjaxResult;
import com.travelshare.common.core.domain.entity.TreeColumnModel;
import com.travelshare.common.enums.BusinessType;
import com.travelshare.business.domain.ColumnDetail;
import com.travelshare.business.service.IColumnDetailService;
import com.travelshare.business.service.IInfoColumnService;
import com.travelshare.common.utils.SecurityUtils;
import com.travelshare.common.utils.poi.ExcelUtil;
import com.travelshare.framework.config.ServerConfig;
import com.travelshare.common.core.page.TableDataInfo;

/**
 * 文章内容详情Controller
 * 
 * @author travelshare
 * @date 2024-02-22
 */
@RestController
@RequestMapping("/business/columnDetail")
public class ColumnDetailController extends BaseController
{
    @Autowired
    private IColumnDetailService columnDetailService;
    
    @Autowired
    private IInfoColumnService infoColumnService;
    
    @Autowired
    private ServerConfig serverConfig;

    /**
     * 查询文章内容详情列表
     */
    @PreAuthorize("@ss.hasPermi('business:columnDetail:list')")
    @GetMapping("/list")
    public TableDataInfo list(ColumnDetail columnDetail)
    {
    	if(!SecurityUtils.hasRole("admin"))  {
    		columnDetail.setUserId(getUserId());
    	}
    	columnDetail.setUserIds(getUserId());
        startPage();
        List<ColumnDetail> list = columnDetailService.selectColumnDetailList(columnDetail);
        return getDataTable(list);
    }

    /**
     * 导出文章内容详情列表
     */
    @PreAuthorize("@ss.hasPermi('business:columnDetail:export')")
    @Log(title = "文章内容详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ColumnDetail columnDetail)
    {
        List<ColumnDetail> list = columnDetailService.selectColumnDetailList(columnDetail);
        ExcelUtil<ColumnDetail> util = new ExcelUtil<ColumnDetail>(ColumnDetail.class);
        util.exportExcel(response, list, "文章内容详情数据");
    }

    /**
     * 获取文章内容详情详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:columnDetail:query')")
    @GetMapping(value = "/{detailId}")
    public AjaxResult getInfo(@PathVariable("detailId") Long detailId)
    {
        return success(columnDetailService.selectColumnDetailByDetailId(detailId));
    }

    /**
     * 文章详情
     * */
    @PreAuthorize("@ss.hasPermi('business:columnDetail:query')")
    @GetMapping(value = "/getInfoForApp/{detailId}")
    public AjaxResult getInfoForApp(@PathVariable("detailId") Long detailId)
    {
    	ColumnDetail columnDetail = columnDetailService.selectColumnDetailByDetailIdAndUserId(detailId, getUserId());
    	String url = serverConfig.getUrl() + "/profile";
    	if(columnDetail.getContent().contains("/profile")) {
    		columnDetail.setContent(columnDetail.getContent().replaceAll("/profile" , url));
    	}
    	if(StringUtils.isNotBlank(columnDetail.getPicPath()) && columnDetail.getPicPath().contains("/profile")) {
    		columnDetail.setPicPath(columnDetail.getPicPath().replaceAll("/profile" , url));
    	}
    	if(StringUtils.isNotBlank(columnDetail.getPicContent()) && columnDetail.getPicContent().contains("/profile")) {
    		columnDetail.setPicContent(columnDetail.getPicContent().replaceAll("/profile" , url));
    	}
        return success(columnDetail);
    }

    /**
     * 新增文章内容详情
     */
    @PreAuthorize("@ss.hasPermi('business:columnDetail:add')")
    @Log(title = "文章内容详情", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ColumnDetail columnDetail)
    {
    	String url = serverConfig.getUrl();
    	columnDetail.setUserId(getUserId());
    	columnDetail.setDelFlag("0");
    	columnDetail.setCreateBy(getUsername());
    	columnDetail.setClickNum(0);
    	if(StringUtils.isNotBlank(columnDetail.getContent()) && columnDetail.getContent().contains(url)) {
    		columnDetail.setContent(columnDetail.getContent().replaceAll(url, ""));
    	}
    	if(StringUtils.isNotBlank(columnDetail.getPicPath()) && columnDetail.getPicPath().contains(url)) {
    		columnDetail.setPicPath(columnDetail.getPicPath().replaceAll(url, ""));
    	}
    	if(StringUtils.isNotBlank(columnDetail.getPicContent()) && columnDetail.getPicContent().contains(url)) {
    		columnDetail.setPicContent(columnDetail.getPicContent().replaceAll(url, ""));
    	}
        return toAjax(columnDetailService.insertColumnDetail(columnDetail));
    }

    /**
     * 修改文章内容详情
     */
    @PreAuthorize("@ss.hasPermi('business:columnDetail:edit')")
    @Log(title = "文章内容详情", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ColumnDetail columnDetail)
    {
    	if(!SecurityUtils.hasRole("admin")) {
    		if(columnDetail.getUserId().longValue() != getUserId()) {
    			return error("无权限编辑该内容");
    		}
    	}
    	String url = serverConfig.getUrl();
    	if(StringUtils.isNotBlank(columnDetail.getContent()) && columnDetail.getContent().contains(url)) {
    		columnDetail.setContent(columnDetail.getContent().replaceAll(url, ""));
    	}
    	if(StringUtils.isNotBlank(columnDetail.getPicPath()) && columnDetail.getPicPath().contains(url)) {
    		columnDetail.setPicPath(columnDetail.getPicPath().replaceAll(url, ""));
    	}
    	if(StringUtils.isNotBlank(columnDetail.getPicContent()) && columnDetail.getPicContent().contains(url)) {
    		columnDetail.setPicContent(columnDetail.getPicContent().replaceAll(url, ""));
    	}
    	columnDetail.setUpdateBy(getUsername());
        return toAjax(columnDetailService.updateColumnDetail(columnDetail));
    }

    /**
     * 删除文章内容详情
     */
    @PreAuthorize("@ss.hasPermi('business:columnDetail:remove')")
    @Log(title = "文章内容详情", businessType = BusinessType.DELETE)
	@DeleteMapping("/{detailIds}")
    public AjaxResult remove(@PathVariable Long[] detailIds)
    {
    	if(detailIds == null) {
    		return error("请选择删除内容");
    	}
    	if(!SecurityUtils.hasRole("admin")) {
    		Integer count = columnDetailService.checkColumnDetailByDetailIdsAndUserId(detailIds, getUserId());
    		if(count != detailIds.length) {
    			return error("删除内容包含他人的文章，您无权删除！");
    		}
    	}
        return toAjax(columnDetailService.deleteColumnDetailByDetailIds(detailIds));
    }
    
    @GetMapping("/columnTree")
    public AjaxResult columnTree(TreeColumnModel treeColumnModel)
    {
    	if(!SecurityUtils.hasRole("admin"))  {
    		treeColumnModel.setState("Y");
    	}
        return success(infoColumnService.selectColumnTreeList(treeColumnModel));
    }
    
    @GetMapping("/shareList")
    public TableDataInfo shareList(ColumnDetail columnDetail)
    {
    	if(!SecurityUtils.hasRole("admin"))  {
    		columnDetail.setUserId(getUserId());
    	}
    	columnDetail.setIsShare(1);
		columnDetail.setState("Y");
    	columnDetail.setUserIds(getUserId());
        startPage();
        List<ColumnDetail> list = columnDetailService.selectColumnDetailList(columnDetail);
        return getDataTable(list);
    }
    
    /**
     * 个人发布得文章
     * */
    @GetMapping("/getUserList")
    public TableDataInfo getUserList(ColumnDetail columnDetail)
    {
    	columnDetail.setUserId(getUserId());
    	columnDetail.setUserIds(getUserId());
        startPage();
        List<ColumnDetail> list = columnDetailService.selectColumnDetailList(columnDetail);
        return getDataTable(list);
    }
}
